#include "functions.h"

int who_win = -1;// 用来判断赢家 初始值-1 左赢0 右赢1
int score[2]={0};
int ctrl_ball2 = 0;//控制道具
int ctrl_ball3 = 0;//控制道具


// init the pos struct for the function
int main(void)
{
    CONSOLE_CURSOR_INFO cursor_info = {1, 0}; //隐藏光标
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
    animation();    //启动画面
    bool flag = true;
    bool flag_start = true;
    bool flag_score = true;
    //map用于储存现在的位置，pre用于储存上一次位置。
    char map[HEIGHT][WIDTH], pre_map[HEIGHT][WIDTH];
    //score用于储存现在的分数，pre用于储存上一次分数。
    int pre_score1=0 , pre_score2=0;
    //init the map
    //同步两个地图
    init_map(map);
    init_map(pre_map);

    //-------------init board---------------
    int board_pos1[2] = {3, HEIGHT/2}, board_pos2[2] = {WIDTH - 3, HEIGHT/2}; //pos[0]储存x    pos[1]储存y
    
    //-------------init ball----------------
    struct ball ball1 = {
        {1, 2},{1, 1},
        BALL_SPEED
    };
    struct ball ball2 = {
        {1, 1},{1, 1},
        PROP_SPEED_P1
    };
     struct ball ball3 = {
        {30, 1},{-1, 1},
        PROP_SPEED_P2
    };// ball_dirc前一个值代表水平速度的方向右正、左负，后一个代表水平方向速度下正，上负。
    

    //------------ init screen----------
    
    // clear screen
    system("cls");
    // set board
    board(map, board_pos1, board_pos2);
    printout(map);

    //the main loop
    
    while (flag)
    {   
        while(flag_start)
        {
            if(flag_score){
                print_d_num(score);
                flag_score=false;
                }
            flag_start=board_start(map, board_pos1, board_pos2);
            upd_screen(map,pre_map);
            if(!flag_start)
            {
            ball1.pos[Y_POS] = (float)(board_pos1[Y_POS] );  //重置游戏
            ball1.pos[X_POS] = (float)(board_pos1[X_POS]+1);
            ball1.dirc[Y_POS] = 0;
            ball1.dirc[X_POS] = 1;
            init_map(map);
            print_d_num(score);
            flag_score=true;
            break;
            }
        
        }
        // to mark the begin time
        int strart_time = clock();

        board(map, board_pos1, board_pos2);
        flag = ball_upd(map, board_pos1[Y_POS], board_pos2[Y_POS], &ball1);
        
        //两个道具球
        pre_score1=score[0];
        pre_score2=score[1];
        ball_pop_1(map,board_pos1[Y_POS],board_pos2[Y_POS],&ball2);
        ball_pop_2(map,board_pos1[Y_POS],board_pos2[Y_POS],&ball3);
        
        upd_screen(map, pre_map);

        // print info
        gotoxy(0, HEIGHT);
        printf("Speed: %.2f\n", ball1.speed);

        
        score_st(score);
        if(pre_score1!=score[0]||pre_score2!=score[1]) //若分数改变则更新计分板
            print_d_num(score);
        

        //delay to control time
        int extra_time = (1000 / FPS) - (clock() - strart_time);
        delay(extra_time);
        if(!flag)
            {
                system("cls");               
                flag_start = flag = win_ani(flag, map, board_pos1, board_pos2, &ball1, &ball2, &ball3);
               
                while (kbhit())
                    getch();    //clear buffer
            }
                            

    }
    //the main loop
    //inti the list
    
    

    Node *list = NULL;
    list = (Node*) malloc(sizeof(Node));
    list->next = NULL;
    summarize(list, score);
    return 0;
}


void init_map(char map[][WIDTH])
{
    // init map
    for (int i = 0; i < HEIGHT; i++)
    {
        // init the first&last line
        if (i == 0 || i == HEIGHT - 1)
        {
            for (int j = 0; j < WIDTH; j++)
            {
                map[i][j] = '=';
            }
        }
        else
        {
            // init the wall on left&right
            map[i][0] = '#';
            map[i][WIDTH - 1] = '#';
            for (int j = 1; j < WIDTH - 1; j++)
            {
                map[i][j] = ' ';
            }
        }
    }
    //init map
}

void board(char map[][WIDTH], int pos1[], int pos2[])
{
    init_map(map);
    static short key_w=0 , key_s=0 , key_8=0 , key_5=0 ,key_up=0, key_down=0;
    int save1y= pos1[Y_POS],
        save2y= pos2[Y_POS];
    if (kbhit())
    {
        key_w += GetKeyState(0x57) < 0;
        key_s += GetKeyState(0x53) < 0;
        key_8 += GetKeyState(0x68) < 0;
        key_5 += GetKeyState(0x65) < 0;
        key_up += GetKeyState(0x26) < 0;
        key_down += GetKeyState(0x28) < 0;
        if (key_w>=2 && pos1[Y_POS] != 2)       /*p1：ws上下*/
            {pos1[Y_POS] -= 1;
            key_w=0;}                /*p2：85或↑ ↓上下*/
        if ((key_8>=2||key_up>=2) && pos2[Y_POS] != 2)
            {pos2[Y_POS] -= 1;
            key_8=key_up=0;}
        if (key_s>=2 && pos1[Y_POS] != 17)
            {pos1[Y_POS] += 1;
            key_s=0;}
        if ((key_5>=2||key_down>=2) && pos2[Y_POS] != HEIGHT -3 )
            {pos2[Y_POS] += 1;
            key_5=key_down=0;}
    }

    for (int i = -1; i != 2; i++)
        map[save1y + i][pos1[X_POS]] = map[save2y + i][pos2[X_POS]] = ' '; /*清空原来的板*/
    for (int i = -1; i != 2; i++)
        map[pos1[Y_POS] + i][pos1[X_POS]] = map[pos2[Y_POS] + i][pos2[X_POS]] = '|'; /*放上新的板*/

}

bool board_start(char map[][WIDTH], int pos1[], int pos2[])
{
    init_map(map);
    static short key_w=0 , key_s=0 , key_8=0 , key_5=0 ,key_up=0, key_down=0 ;
    short key_D=0;
    int save1y= pos1[Y_POS],
        save2y= pos2[Y_POS];
    if (kbhit())
    {
        key_w += GetKeyState(0x57) < 0;
        key_s += GetKeyState(0x53) < 0;
        key_8 += GetKeyState(0x68) < 0;
        key_5 += GetKeyState(0x65) < 0;
        key_up += GetKeyState(0x26) < 0;
        key_down += GetKeyState(0x28) < 0;
        key_D += GetKeyState(0x44) < 0;
        if (key_w>=315 && pos1[Y_POS] != 2)       /*p1：ws上下*/
            {pos1[Y_POS] -= 1;
            key_w=0;}                /*p2：85或↑ ↓上下*/
        if ((key_8>=315||key_up>=315) && pos2[Y_POS] != 2)
            {pos2[Y_POS] -= 1;
            key_8=key_up=0;}
        if (key_s>=315 && pos1[Y_POS] != 17)
            {pos1[Y_POS] += 1;
            key_s=0;}
        if ((key_5>=315||key_down>=315) && pos2[Y_POS] != HEIGHT -3)
            {pos2[Y_POS] += 1;
            key_5=key_down=0;}
    }

    for (int i = -1; i != 2; i++)
        map[pos1[Y_POS] + i][pos1[X_POS]] = map[pos2[Y_POS] + i][pos2[X_POS]] = '|'; /*放上新的板*/
    
    map[pos1[Y_POS]][pos1[X_POS]+1]='@';

    return (key_D!=1);
}

void gotoxy(int m, int n) //类似于清屏函数，光标移动到原点位置进行重画
{
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD pos;
    pos.X = m;
    pos.Y = n;
    SetConsoleCursorPosition(handle, pos);
}
